﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FluentData;
using Framework.Data;

namespace Framework.Core
{
    /// <summary>
    /// 房型
    /// </summary>
    [Serializable]
    public class RoomType
    {
        /// <summary>
        /// 房型编码
        /// </summary>
        public string RoomTypeCode { get; set; }

        /// <summary>
        /// 房型名称
        /// </summary>
        public string RoomTypeName { get; set; }

        /// <summary>
        /// 索引子
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public RoomType this[string key]
        {
            get
            {
                return RoomType.Load().Find(s => s.RoomTypeCode == key);
            }
        }

        //TODO: 将所有房型缓存
        private static List<RoomType> allRoomTypes; 
        /// <summary>
        /// 加载入所有房型.
        /// </summary>
        /// <returns></returns>
        public static List<RoomType> Load()
        {
            if (allRoomTypes == null)
            {
                using (var context = DataHelper.GetBaseDbContext())
                {
                    var sql = "SELECT sParameterID, sScript FROM Parameter_Info WHERE bIsValid=1 AND sParameterTypeID='RoomTypeID'";

                    allRoomTypes = context.Sql(sql).QueryMany<RoomType>(delegate(RoomType roomType, IDataReader row)
                            {
                                roomType.RoomTypeCode = row.GetString("sParameterID");
                                roomType.RoomTypeName = row.GetString("sScript");
                            });
                }
            }

            return allRoomTypes;
        }

        /// <summary>
        /// 刷新实时从数据库更新最新
        /// </summary>
        /// <returns></returns>
        public static List<RoomType> Reload()
        {
            allRoomTypes = null;
            return Load();
        }
    }
}
